import { createRouter, createWebHashHistory } from 'vue-router'

import { useMenuStore } from '@/stores/menuStore'
import { useUserStore } from '@/stores/userStore'

const Iframe = () => import('@/components/iframe/index.vue')

const NoAuth = () => import('@/views/no-auth/index.vue')

const Login = () => import('@/views/login/index.vue')
const Main = () => import('@/views/main/index.vue')
const Home = () => import('@/views/home/index.vue')
const UserInfo = () => import('@/views/user/user-info.vue')
const RevisePwd = () => import('@/views/user/revise-pwd.vue')
const UserMessage = () => import('@/views/user/user-message.vue')

const Dict = () => import('@/views/system-config/dict/index.vue')
const Params = () => import('@/views/system-config/params/index.vue')

const FileManage = () => import('@/views/system-config/file-manage/index.vue')
const StaticFileManage = () => import('@/views/system-config/static-file-manage/index.vue')

const User = () => import('@/views/system-config/user/index.vue')
const Org = () => import('@/views/system-config/org/index.vue')
const Role = () => import('@/views/system-config/role/index.vue')
const Menu = () => import('@/views/system-config/menu/index.vue')
const Auth = () => import('@/views/system-config/auth/index.vue')
const PasswordStrategy = () => import('@/views/system-config/password-strategy/index.vue')


const OnlineUser = () => import('@/views/system-monitor/online-user/index.vue')
const RemoteServer = () => import('@/views/system-monitor/remote-server/index.vue')
const RemoteServerSsh = () => import('@/views/system-monitor/remote-server/ssh.vue')
const JobSchedule = () => import('@/views/system-monitor/job-schedule/index.vue')
const JobLog = () => import('@/views/system-monitor/job-schedule/job-log.vue')
const ServerMonitor = () => import('@/views/system-monitor/server-monitor/index.vue')
const ZkManager = () => import('@/views/system-monitor/zk-manager/index.vue')

const CodeGenerator = () => import('@/views/system-tools/code-generator/index.vue')
const Tables = () => import('@/views/system-tools/code-generator/tables.vue')
const Columns = () => import('@/views/system-tools/code-generator/columns.vue')
const CodePreview = () => import('@/views/system-tools/code-generator/code-preview.vue')
const FormGenerator = () => import('@/views/system-tools/form-generator/index.vue')

const LoginLog = () => import('@/views/system-log/login-log/index.vue')
const OperateLog = () => import('@/views/system-log/operate-log/index.vue')

const WorkflowModelManage = () => import('@/views/workflow/workflow-model-manage/index.vue')
const WorkflowInfo = () => import('@/views/workflow/workflow-info/index.vue')
const WorkflowStart = () => import('@/views/workflow/workflow-start/index.vue')
const WorkflowProcess = () => import('@/views/workflow/workflow-process/index.vue')
const WorkflowMyProcess = () => import('@/views/workflow/workflow-my-process/index.vue')
const WorkflowMyStart = () => import('@/views/workflow/workflow-my-start/index.vue')
const WorkflowMonitor = () => import('@/views/workflow/workflow-monitor/index.vue')

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    { name: 'login', path: '/login', component: Login },
    {
      name: 'main',
      path: '/',
      component: Main,
      children: [
        { name: 'iframe', path: '/iframe/:menuCode', component: Iframe, props: true },
        { name: 'no-auth', path: '/no-auth', component: NoAuth },
        { name: 'home', path: '/home', component: Home },
        { name: 'user-info', path: '/user-info', component: UserInfo },
        { name: 'user-message', path: '/user-message', component: UserMessage },
        { name: 'revise-pwd', path: '/revise-pwd', component: RevisePwd },
        {
          name: 'system-config',
          path: '/system-config',
          children: [
            { name: 'user', path: '/system-config/user', component: User },
            { name: 'org', path: '/system-config/org', component: Org },
            { name: 'role', path: '/system-config/role', component: Role },
            { name: 'auth', path: '/system-config/auth', component: Auth },
            { name: 'params', path: '/system-config/params', component: Params },
            { name: 'dict', path: '/system-config/dict', component: Dict },
            { name: 'menu', path: '/system-config/menu', component: Menu },
            { name: 'file-manage', path: '/system-config/file-manage', component: FileManage },
            { name: 'static-file-manage', path: '/system-config/static-file-manage', component: StaticFileManage },
            { name: 'password-strategy', path: '/system-config/password-strategy', component: PasswordStrategy }
          ]
        },
        {
          name: 'system-monitor',
          path: '/system-monitor',
          children: [
            { name: 'login-log', path: '/system-log/login-log', component: LoginLog },
            { name: 'operate-log', path: '/system-log/operate-log', component: OperateLog },
            { name: 'online-user', path: '/system-monitor/online-user', component: OnlineUser },
            { name: 'remote-server', path: '/system-monitor/remote-server', component: RemoteServer },
            { name: 'remote-server-ssh', path: '/system-monitor/remote-server/ssh', component: RemoteServerSsh },
            { name: 'job-schedule', path: '/system-monitor/job-schedule', component: JobSchedule },
            { name: 'job-log', path: '/system-monitor/job-schedule/job-log', component: JobLog },
            { name: 'server-monitor', path: '/system-monitor/server-monitor', component: ServerMonitor },
            { name: 'zk-manager', path: '/system-monitor/zk-manager', component: ZkManager }
          ]
        },
        {
          name: 'system-tools',
          path: '/system-tools',
          children: [
            { name: 'code-generator', path: '/system-tools/code-generator', component: CodeGenerator },
            { name: 'code-generator-tables', path: '/system-tools/code-generator/tables', component: Tables },
            { name: 'code-generator-columns', path: '/system-tools/code-generator/columns', component: Columns },
            { name: 'code-generator-code-preview', path: '/system-tools/code-generator/code-preview', component: CodePreview },
            { name: 'form-generator', path: '/system-tools/form-generator', component: FormGenerator }
          ]
        },
        {
          name: 'workflow',
          path: '/workflow',
          children: [
            { name: 'workflow-model-manage', path: '/workflow/workflow-model-manage', component: WorkflowModelManage },
            { name: 'workflow-info', path: '/workflow/workflow-info', component: WorkflowInfo },
            { name: 'workflow-start', path: '/workflow/workflow-start', component: WorkflowStart },
            { name: 'workflow-process', path: '/workflow/workflow-process', component: WorkflowProcess },
            { name: 'workflow-my-process', path: '/workflow/workflow-my-process', component: WorkflowMyProcess },
            { name: 'workflow-my-start', path: '/workflow/workflow-my-start', component: WorkflowMyStart },
            { name: 'workflow-monitor', path: '/workflow/workflow-monitor', component: WorkflowMonitor }
          ]
        }
      ]
    },
  ]
})

const whiteList: string[] = [
  "/login",
  "/user-info",
  "/user-message",
  "/revise-pwd",
  "/home",
  "/no-auth"
]

router.beforeEach((to, from) => {
  if (whiteList.includes(to.path)) {
    const menuStore = useMenuStore()
    let arr: any[] = []
    for (let i = 0; i < to.matched.length; i++) {
      arr.push(to.matched[i].name)
    }
    menuStore.refreshBreadcrumbList(arr)
    return true
  }

  const userStore = useUserStore()
  userStore.loadAccountInfo()
  userStore.setCurrentPageName(to.name as string)
  userStore.setCurrentPageQuery(to.query)
  if (!userStore.token || userStore.token === "") {
    return { name: 'login' }
  }
  if (to.name === "main") {
    return { name: "home" }
  }
  if (to.name === "iframe" && !userStore.hasAuth(to.params.menuCode ? to.params.menuCode as string : "")) {
    return { name: 'no-auth' }
  }
  if (to.name !== "iframe" && !userStore.hasAuth(to.name ? to.name as string : "")) {
    return { name: 'no-auth' }
  }
  if (to.name === "iframe") {
    const menuStore = useMenuStore()
    let arr: any[] = []
    arr.push("main")
    arr.push(to.params.menuCode)
    menuStore.refreshBreadcrumbList(arr)
  } else {
    const menuStore = useMenuStore()
    let arr: any[] = []
    for (let i = 0; i < to.matched.length; i++) {
      arr.push(to.matched[i].name)
    }
    menuStore.refreshBreadcrumbList(arr)
  }
})

router.afterEach((to, from) => {

})

export default router
